package com.lanou3g.code0512.request;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Created by dllo on 18/5/12.
 */
@WebServlet(name = "RequestServlet", urlPatterns = "/search")
public class RequestServlet extends HttpServlet {

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        response.setContentType("text/html;charset=utf-8");// 文字格式必须加
        String keyWord = request.getParameter("keyword");
       // 在获取参数之前 告诉Tomcat 参数是utf8编码
        // 在get请求中 这种方式不行
       // request.setCharacterEncoding("utf-8");

        // 使用iso-8859-1转换回原始二进制
        // 再由原始二进制使用正确编码转化成字符串
//        byte[] bytes = keyWord.getBytes("iso-8859-1");
//        keyWord = new String(bytes, "utf-8");
        response.getWriter().println("用户输入的是:" + keyWord);
    }
}
